package txtReade;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

import uci.utils.RunningTime;

public class TxtReader
{
	public static void main(String[] args) throws IOException
	{
		//testSkip();
		//weight100Lines();
		//readChunk();
		random();
		
			
			
		
	}

	private static void random()throws IOException
	{
		File f = new File("c:/data/McBatch_stdout.2013-06-17_00-17-02_27141.log");	
		RandomAccessFile raf = new RandomAccessFile(f,"r");
		long rafLong= raf.length();
		long index = (rafLong-15000);
		int steps=(int) (rafLong/15000);
		System.out.println(index);
		raf.seek(index);
		String txt = "";
		String strLine;
		StringBuffer sb = new StringBuffer();		
		while ((strLine = raf.readLine()) != null)
		{
			sb.append(strLine);
		}
		txt=sb.toString();
		
	}

	private static void readChunk() throws IOException
	{
		File f = new File("c:/data/McBatch_stdout.2013-06-17_00-17-02_27141.log");	
		long sizeMem = f.length();
		long index = (sizeMem-15000);
		FileInputStream fstream = new FileInputStream(f);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		br.skip(index);
		String strLine;
		String txt = "";
		StringBuffer sb = new StringBuffer();		
		while ((strLine = br.readLine()) != null)
		{
			sb.append(strLine);
		}
		txt=sb.toString();
		System.out.println(txt);
		System.out.println( "index:"+index+"\tsizeMem="+sizeMem+" \ttxt.length="+txt.length());
		
		
	}

	private static void weight100Lines() throws IOException
	{
		File f = new File("c:/data/McBatch_stdout.2013-06-17_00-17-02_27141.log");
		FileInputStream fstream = new FileInputStream(f);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));

		String strLine;
		StringBuffer sb = new StringBuffer();	
		int totalLines=1;
		while ((strLine = br.readLine()) != null)
		{
			sb.append(strLine);
			if(totalLines%100==0){
				System.out.println(totalLines+";"+sb.toString().length());
				sb = new StringBuffer();
			}

			totalLines++;
		}
	}

	private static void testSkip() throws IOException
	{
		File f = new File("c:/data/McBatch_stdout.2013-06-17_00-17-02_27141.log");
		//f = new File("c:/data/test.txt");
		long sizeMem = f.length();
		RunningTime rt = new RunningTime();
		for(int j=0;j<11;j++){
			rt.start();
			long i =(long) (sizeMem*(1-(double)j/10.));
			//i=0;
			FileInputStream fstream = new FileInputStream(f);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			br.skip(i);
			String strLine;
			String txt = "";
			StringBuffer sb = new StringBuffer();		
			while ((strLine = br.readLine()) != null)
			{
				sb.append(strLine);
			}
			txt=sb.toString();
			//System.out.println(txt);
			rt.stop();
			System.out.println(j+" skip:"+i+" \t rt:"+rt+"\tsizeMem="+sizeMem+" \ttxt.length="+txt.length());
			
		}
		
	}
}
